package plugins.packagejava;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import domain.model.project.ProjectDeveloper;
import domain.model.project.ProjectType;
import infra.jenkins.JenkinsRule;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Gregory Boissinot
 */
public class JavaPackageProjectTypeImpl implements RWJavaPackageProjectType {

    private static final String DEFAULT_JAVA_PACKAGE_TYPE_NAME = "packageJava";

    @JsonProperty(required = false)
    private List<ProjectDeveloper> projectDeveloperList = Collections.EMPTY_LIST;


    @Override
    @JsonIgnore
    public String getName() {
        return DEFAULT_JAVA_PACKAGE_TYPE_NAME;
    }


    @Override
    public List<ProjectDeveloper> getProjectDeveloperList() {
        return Collections.unmodifiableList(projectDeveloperList);
    }

    @Override
    public ProjectType copyWithProjectDevelopers(List<ProjectDeveloper> projectDevelopers) {
        return new JavaPackageProjectTypeBuilder()
                .projectDeveloperList(projectDevelopers)
                .build();
    }


    @Override
    public void setProjectDeveloperList(List<ProjectDeveloper> projectDeveloperList) {
        this.projectDeveloperList = projectDeveloperList;
    }

    @JsonIgnore
    @Override
    public Map<String, Object> getMetaInformation() {
        Map<String, Object> mapInfo = new HashMap<String, Object>();
        mapInfo.put(JenkinsRule.JOB_EXTENSION, "PKG");
        return mapInfo;
    }

    @Override
    public boolean sameValueAs(ProjectType objectValue) {
        return equals(objectValue);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        JavaPackageProjectTypeImpl that = (JavaPackageProjectTypeImpl) o;

        if (projectDeveloperList != null ? !projectDeveloperList.equals(that.projectDeveloperList) : that.projectDeveloperList != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        return projectDeveloperList != null ? projectDeveloperList.hashCode() : 0;
    }

    @Override
    public String toString() {
        final ReflectionToStringBuilder reflectionToStringBuilder = new ReflectionToStringBuilder(this,
                ToStringStyle.MULTI_LINE_STYLE);
        return reflectionToStringBuilder.toString();
    }
}
